﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PMS.Controllers.BD
{
    public class ComboPickController : Controller
    {
        //
        // GET: /ComboPick/

        public ActionResult Index()
        {
            return View();
        }
        public void GetComboList()
        {
            var list = BLLSession.Current.BD_ComboPickUserBLL.GetPagedList(OperateContext.Page, OperateContext.Rows, c => 1 == 1, u => u.ID, OperateContext.Order);
            int count = BLLSession.Current.BD_ComboPickUserBLL.GetCount(c => 1 == 1);
            Response.Write(JsonHelper.toPageJson(list, count));
        }

        public void AddCombo()
        {
            BD_ComboPickUser combo = new BD_ComboPickUser() { FillName = OperateContext.sessUserInfo.Name, FillDate = DateTime.Now.ToString("yyyy-MM-dd") };
            ModelTools.Form2EditModel(Request.Form, combo);
            BLLSession.Current.BD_ComboPickUserBLL.Add(combo);
            Response.Write(JsonHelper.toDefaultMsg(true, "添加成功"));

        }

        public void EditCombo()
        {
            int Id = Commons.String2Int32(Request.Form["Id"]);
            BD_ComboPickUser combo = BLLSession.Current.BD_ComboPickUserBLL.GetListBy(c => c.ID == Id).FirstOrDefault();
            ModelTools.Form2EditModel(Request.Form, combo);
            combo.FillDate = DateTime.Now.ToString("yyyy:MM:dd,HH:mm:ss");
            combo.FillName = OperateContext.sessUserInfo.Name;
            BLLSession.Current.BD_ComboPickUserBLL.ModifyAllModel(combo);
            Response.Write(JsonHelper.toDefaultMsg(true, "修改成功"));
        }

        public void RemoveCombo()
        {
            if (Request.Form["Id"] != null)
            {
                int Id = Commons.String2Int32(Request.Form["Id"]);
                BLLSession.Current.BD_ComboPickUserBLL.DelBy(c => c.ID == Id);
                Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
            }
        }

        public void GetComboPerTree()
        {
            int comboId = Convert.ToInt32(Request.Form["Id"]);
            string action = Request["action"].ToString();
            if (action == "low")
            {
                List<BD_DepInfo> allDep = BLLSession.Current.BD_DepInfoBLL.GetListBy(d => d.DepState == "0").ToList();
                var allUser = UserRelate.GetAllUsers();
                List<int> checkUser = BLLSession.Current.BD_RevUserInfoBLL.GetListBy(c => c.depRevInfoId == comboId).Select(c => c.userId).ToList();
                List<TreeNode> node = UserRelate.GetComboUserTree(allDep, allUser, checkUser);
                string nodeStr = JsonHelper.toJson(node);
                string newNodeStr = nodeStr.Replace("Checked", "checked");
                Response.Write(JsonHelper.toJson(newNodeStr));
            }
            else if (action == "high")
            {
                List<BD_DepInfo> allDep = BLLSession.Current.BD_DepInfoBLL.GetListBy(d => d.DepState == "0").ToList();
                var allUser = UserRelate.GetAllUsers();
                List<int> checkUser = BLLSession.Current.BD_RevDepInfoBLL.GetListBy(c => c.ledRevInfoId == comboId).Select(c => c.userId).ToList();
                List<TreeNode> node = UserRelate.GetComboUserTree(allDep, allUser, checkUser);
                string nodeStr = JsonHelper.toJson(node);
                string newNodeStr = nodeStr.Replace("Checked", "checked");
                Response.Write(JsonHelper.toJson(newNodeStr));
            }
            else if (action == "SchoolHead")
            {
                string schoolcode = Request.Form["schoolcode"].ToString();
                //List<BD_DepInfo> allDep = BLLSession.Current.BD_DepInfoBLL.GetListBy(d => d.DepState == "0"&&).ToList();
                List<View_BD_UserDepInfo> allUser = DALSession.Current.View_BD_UserDepInfoDAL.GetListBy(u => u.UserState == "0" && u.SchoolCode == schoolcode).ToList();
                List<int> depid = allUser.GroupBy(p => new { p.DepId }).Select(g => g.First().DepId).ToList();
                List<BD_DepInfo> allDep = BLLSession.Current.BD_DepInfoBLL.GetListBy(d => d.DepState == "0" && depid.Contains(d.ID)).ToList();
                List<int> checkUser = BLLSession.Current.BD_SchoolHeadInfoBLL.GetListBy(c => c.SchoolInfoId == comboId).Select(c => c.userId).ToList();
                List<TreeNode> node = UserRelate.GetComboUserTree(allDep, allUser, checkUser);
                string nodeStr = JsonHelper.toJson(node);
                string newNodeStr = nodeStr.Replace("Checked", "checked");
                Response.Write(JsonHelper.toJson(newNodeStr));
            }
            else
            {
                List<BD_DepInfo> allDep = BLLSession.Current.BD_DepInfoBLL.GetListBy(d => d.DepState == "0").ToList();
                var allUser = UserRelate.GetAllUsers();
                List<int> checkUser = BLLSession.Current.BD_ComboUserBLL.GetListBy(c => c.ComboId == comboId).Select(c => c.UserId).ToList();
                List<TreeNode> node = UserRelate.GetComboUserTree(allDep, allUser, checkUser);
                string nodeStr = JsonHelper.toJson(node);
                string newNodeStr = nodeStr.Replace("Checked", "checked");
                Response.Write(JsonHelper.toJson(newNodeStr));
            }

        }

        public void EditComboPeoTree()
        {
            int comboId = Convert.ToInt32(Request.Form["Id"]);
            string action = Request["action"].ToString();
            List<int> nodes = new List<int>();
            string nodesStr = Request.Form["SubCombo"].ToString();
            string reviewer = "";
            for (int i = 0; i < nodesStr.Trim(',').Split(',').Length; i++)
            {
                nodes.Add(Commons.String2Int32(nodesStr.Trim(',').Split(',')[i]));
            }
            if (action == "low")
            {
                BLLSession.Current.BD_RevUserInfoBLL.DelBy(p => p.depRevInfoId == comboId);
                foreach (var pId in nodes)
                {
                    string userName = BLLSession.Current.BD_UserInfoBLL.GetListBy(d => d.ID == pId).FirstOrDefault().UserName;
                    BLLSession.Current.BD_RevUserInfoBLL.Add(new BD_RevUserInfo() { depRevInfoId = comboId, userId = pId, userName = userName });
                    reviewer = reviewer + ',' + userName;
                }
                BD_DepRevInfo depRev = new BD_DepRevInfo();
                depRev.reviewer = reviewer;
                BLLSession.Current.BD_DepRevInfoBLL.ModifyBy(depRev, d => d.ID == comboId, "reviewer");
            }
            else if (action == "high")
            {
                BLLSession.Current.BD_RevDepInfoBLL.DelBy(p => p.ledRevInfoId == comboId);
                foreach (var pId in nodes)
                {
                    string userName = BLLSession.Current.BD_UserInfoBLL.GetListBy(d => d.ID == pId).FirstOrDefault().UserName;
                    BLLSession.Current.BD_RevDepInfoBLL.Add(new BD_RevDepInfo() { ledRevInfoId = comboId, userId = pId, userName = userName });
                    reviewer = reviewer + ',' + userName;
                }
                BD_LedRevInfo depRev = new BD_LedRevInfo();
                depRev.reviewer = reviewer;
                BLLSession.Current.BD_LedRevInfoBLL.ModifyBy(depRev, d => d.ID == comboId, "reviewer");
            }
            else if (action == "SchoolHead")
            {
                BLLSession.Current.BD_SchoolHeadInfoBLL.DelBy(p => p.SchoolInfoId == comboId);
                BD_SchoolInfo school = BLLSession.Current.BD_SchoolInfoBLL.GetListBy(u => u.ID == comboId).FirstOrDefault();
                foreach (var pId in nodes)
                {
                    BD_UserInfo info = BLLSession.Current.BD_UserInfoBLL.GetListBy(d => d.ID == pId).FirstOrDefault();
                    string userName = info.UserName;
                    BLLSession.Current.BD_SchoolHeadInfoBLL.Add(new BD_SchoolHeadInfo() { SchoolInfoId = comboId, SchoolCode = school.SchoolCode, userId = pId, userName = userName });
                    reviewer = reviewer + ',' + userName;
                    string schoolcodes = "";
                    List<BD_SchoolHeadInfo> headlist = BLLSession.Current.BD_SchoolHeadInfoBLL.GetListBy(u => u.userId == info.ID);
                    for (int i = 0; i < headlist.Count; i++)
                    {
                        schoolcodes += headlist[i].SchoolCode + ",";
                    }
                    info.ChargeSchoolCodes = schoolcodes;
                    BLLSession.Current.BD_UserInfoBLL.ModifyAllModel(info);
                }
                BD_SchoolInfo depRev = new BD_SchoolInfo();
                depRev.HeadIDList = nodesStr.Trim(',');
                depRev.HeadNameList = reviewer;
                BLLSession.Current.BD_SchoolInfoBLL.ModifyBy(depRev, d => d.ID == comboId, "HeadIDList", "HeadNameList");
            }
            else
            {
                BLLSession.Current.BD_ComboUserBLL.DelBy(p => p.ComboId == comboId);
                foreach (var pId in nodes)
                {
                    BLLSession.Current.BD_ComboUserBLL.Add(new BD_ComboUser() { ComboId = comboId, UserId = pId });
                }
            }
            Response.Write(JsonHelper.toDefaultMsg(true));
        }
    }
}
